package com.shop.api;

import com.shop.po.R;
import com.shop.po.dto.OrderDetailInfo;
import com.shop.user.po.dto.OrderInfo;
import com.shop.user.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author banxia
 * @since 2023-02-25
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取订单表的详细情况
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable("orderId") Long orderId){
        OrderInfo orderInfo = orderService.getOrderInfo(orderId);
        return R.ok().data("orderInfo",orderInfo);
    }


    /**
     * 为订单选择优惠券
     * @param orderId：这里是指主键
     *代开发，暂时无法实现。。
     * @param discountId
     */
    @PostMapping("/choiceDiscount/{orderId}/{discountId}")
    public R choiceDiscount(@PathVariable("orderId") Long orderId,
                               @PathVariable("discountId") Long discountId){
        orderService.choiceDiscount(orderId,discountId);
        return R.ok("成功设置订单优惠券");
    }
    /**
     * 生成订单接口
     * @param cartIdListString:注意：这里是用,分割各个cartID
     * @param addressId : 收获地址ID
     * @param remarks:备注
     */
    @PostMapping("/createOrder/{cartIdListString}")
    public R createOrder(@PathVariable(value = "cartIdListString") String cartIdListString,
                            @RequestParam(value = "addressId",required = true)Long addressId,
                            @RequestParam(value = "remarks",required = false) String remarks){
        //将传入的cartIdListString解析为List<Long>类型
        String[] cartIds = cartIdListString.split(",");
        List<Long> cartIdList=new ArrayList<Long>(cartIds.length);
        for (String cartId : cartIds) {
            cartIdList.add(Long.parseLong(cartId));
        }
        orderService.createOrder(cartIdList,addressId,remarks);
        return R.ok("订单已生成，请尽快支付，30分钟未支付将被取消");
    }

    @GetMapping("/getUserOrders/{state}")
    public R getUserOrders(@PathVariable(value = "state",required = false) String state){
        List<OrderInfo> list=orderService.getUserOrders(state);
        return R.ok().data("list",list);
    }

    @PostMapping("/pay")
    public R  pay(Long id){
        orderService.pay(id);
        return R.ok();
    }

    /**
     * 用于商家获取到对应的订单
     * @param state：订单的状态
     * @return
     */
    @GetMapping("/merchantGetOrderInfo")
    public List<OrderDetailInfo> merchantGetOrderInfo(
            @RequestParam(value = "merchantId",required = true) Long merchantId,
            @RequestParam(value = "state",required = false) String state,
            @RequestParam(value = "currentPage",required = true) Long page,
            @RequestParam(value = "size",required = true) Long size
            ){
        List<OrderDetailInfo> list=orderService.merchantGetOrderInfo(merchantId,state,page,size);
        return list;
    }

    @GetMapping("/merchantGetTotal")
    public Long merchantGetTotal(
            @RequestParam(value = "merchantId",required = true) Long merchantId,
            @RequestParam(value = "state",required = false) String state
    ){
        return orderService.merchantGetTotal(merchantId,state);
    }

    @PostMapping("/changState/{state}/{orderDetailsId}")
    public R changState(@PathVariable("state")String state,
                        @PathVariable("orderDetailsId") String orderDetailsId){
        orderService.changState(state, orderDetailsId);
        return R.ok();
    }

}

